package com.mitac.mcd.cs.mobiletracking.socketserver.util;

public class PointInPolygon {

	public static boolean pointInPolygon(String[] points,double lat,double lon)
	{
	  int i;
	  int j = points.length-1;
	  boolean inPoly = false;

	  for (i=0; i<points.length; i++)
	  {
	    if (getLon(points[i])<lon && getLon(points[j])>=lon
	      || getLon(points[j])<lon && getLon(points[i])>=lon)
	    {
	      if (getLat(points[i])+(lon-getLon(points[i]))/
	        (getLon(points[j])-getLon(points[i]))*(getLat(points[j])
	          -getLat(points[i]))<lat)
	      {
	        inPoly=!inPoly;
	      }
	    }
	    j=i;
	  }
	  return inPoly;
	}
	
	private static double getLon(String coordinate){
		if(coordinate==null||coordinate.equals("")){
			return 0.0;
		}
		String[] temp = coordinate.split("#");
		return Double.parseDouble(temp[1]);
	}
	
	private static double getLat(String coordinate){
		if(coordinate==null||coordinate.equals("")){
			return 0.0;
		}
		String[] temp = coordinate.split("#");
		return Double.parseDouble(temp[0]);
	}
	
	public static void main(String args[]){
		String[] polygon = new String[]{"30.66962503917051#104.0740377905604","30.66711495884018#104.07219243075815",
				"30.66704113196094#104.07064747836557","30.658439914468666#104.07094788577524",
				"30.658439914468666#104.07352280642954","30.66013806955663#104.07360863711801",
				"30.659916572758917#104.07450985934702","30.66726261242943#104.07918763186899"};
		boolean flag = pointInPolygon(polygon,30.667742485036168,104.07588315036264);
		System.out.println(flag);
	}
}
